package com.amigo.online.provider.manager.util.video;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import sun.misc.BASE64Encoder;

public class ImageUtil {
    /**
     * 创建一张制定宽高的黑色图片
     *
     * @param width
     * @param height
     * @throws IOException
     */
    public static void createImage(int width, int height) throws IOException {
        File file = new File("D:/image/createImage.jpg");
        BufferedImage bufferedImage = new BufferedImage(
                width,
                height,
                BufferedImage.TYPE_INT_RGB
        );
        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
        graphics2D.setBackground(Color.BLACK);
        graphics2D.clearRect(0, 0, width, height);
        // 1.将图片写到实体图片里
        ImageIO.write(bufferedImage, "jpg", file);
        // 2.将图片写到流里
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
        // 3.将图片以base64的形式展示
        BASE64Encoder base64Encoder = new BASE64Encoder();
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        System.out.println("data:image/png;base64," +
                base64Encoder.encodeBuffer(byteArray).trim());
        ;
    }

    public static void main(String[] args) {
        try {
            createImage(100, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
